import java.util.ArrayList;

/**
 * This Record class adds the vertices as it
 * list, and updates the path as going along.  
 * @author ldong
 *
 */
public class Record {
     private int pathDistance;
     private int lastEdgeWeight;
     private ArrayList<Vertex> list;    

     /**
      * First Constructor 
      * @param v
      */
     public Record(Vertex v)
     {
    	 lastEdgeWeight = 0;
    	 pathDistance = 0;
         list = new ArrayList<Vertex>();
         addToPath(v);
     }
     
     /**
      * Second Constructor 
      */ 
     public Record(Record rec)
     {
       	 pathDistance = rec.pathDistance;
    	 lastEdgeWeight = rec.lastEdgeWeight;
         list = new ArrayList<Vertex>();

    	 // deep copy of rec's list
    	 for(int i = 0; i < rec.size() ;i++)
    		 list.add(rec.getVertex(i));

     }
     
     /**
      * Third Constructor
      */
     public Record(Record rec, Vertex v)
     {
         this(rec);
         addToPath(v);
     }
     /*****  End of Constructors *****/
     
     /**
      * Add this vertex to path
      * @param v
      * @return
      */
     public void addToPath(Vertex v)
     {
    		 list.add(v);
    		 if(list.size() > 1)
        	  updateDistance(v);
     }

     /**
      * Update the total distance and the last edge weight
      * @param v
      * @return
      */
     public boolean updateDistance(Vertex v)
     {
    	 // update the last edge weight
    	 if(returnLast2Vertex().getAdjacentVertexWeightTable().containsKey(v))
    	 {
    		 lastEdgeWeight = returnLast2Vertex().findWeightFrom(v);
    		 // update the total path pathDistance
        	 pathDistance += lastEdgeWeight;
        	 return true;
    	 }
    	 return false;
     }
 
	 /**
	  * Return the total size of the array     
	  * @return
	  */
     public int size()
     {
    	 return list.size();
     }
     
     /**
      * Return the starting vertex
      * @return
      */
     public Vertex returnFirstVertex()
     {
    	 return list.get(0);
     }
     /**
      * Return the ending vertex
      * @return
      */
     public Vertex returnLastVertex()
     {
    	 return list.get(list.size()-1);
     }
     
     /**
      * Return the second of last vertex
      * @return
      */
     public Vertex returnLast2Vertex()
     {
    	 return list.get(list.size()-2);
     }
     
	/**
	 * Return vertex of certain index     
	 * @param index
	 * @return
	 */
     public Vertex getVertex(int index)
     {
    	 return list.get(index);
     }
     
     /**
      * Overriding toString
      */
	 public String toString()
	 {
	    String temp = "Path: ";
	    for(int i=0; i< list.size(); i++)
	    {
	    	temp += list.get(i).getName() + "->";
	    }
	    temp += "    Distance: " + pathDistance;
	    return temp;
	 }
     
     /*******************************************/
     /************ Setters and Getters  *********/
     /*******************************************/

     public int getPathDistance() {
		return pathDistance;
	}

	public void setDistance(int pathDistance) {
		this.pathDistance = pathDistance;
	}

	public int getLastEdgeWeight() {
		return lastEdgeWeight;
	}

	public void setLastEdgeWeight(int lastEdgeWeight) {
		this.lastEdgeWeight = lastEdgeWeight;
	}

	public ArrayList<Vertex> getList() {
		return list;
	}

	public void setList(ArrayList<Vertex> list) {
		this.list = list;
	}
      
}
